home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Modules / BackSpaceModules / Source / Stef / StefView.m < prev    next >
Text File  |  1993-06-24  |  6KB  |  257 lines

  1. #import "StefView.h"
  2.  
  3. // this code is copyright Darcy Brockbank, 1993
  4. //
  5. // You may freely reuse and distribute this code in any way shape or
  6. // form, provided that this notice stays intact.
  7. //
  8. // darcy@solutions.ca, samurai@cs.mcgill.ca
  9. // StefView was implemented out of WorldSpaceView and retains some of the
  10. // movement properties...
  11. //
  12. // The code for WorldSpaceView was written by Sam Streeper at NeXT, I think,
  13. // and there were two other contributors, but I can't find their names in the
  14. // source for it.
  15. //
  16. // StefView is the common "Spotlight" screen saver you see on Macs and Windows
  17. // all the time. This is just a quick hack, and can do with vast amounts of
  18. // improvement I'm sure.
  19. //
  20. // If you do improve this thing, send me a copy!
  21. //
  22. // - darcy
  23.  
  24. @implementation StefView
  25.  
  26.  
  27. #define X_PERIOD 15000.0
  28. #define Y_PERIOD 12000.0
  29. #define DEFAULT_FPS 20
  30. #define PI 3.1415926535
  31. #define MAX_IMAGE_WIDTH 256
  32. #define MAX_IMAGE_HEIGHT 256
  33. #define MAX_X_SPEED (26) 
  34. #define MAX_Y_SPEED (26) 
  35. #define BUFFER_WIDTH (MAX_IMAGE_WIDTH + MAX_X_SPEED + 1)
  36. #define BUFFER_HEIGHT (MAX_IMAGE_HEIGHT + MAX_Y_SPEED + 1)
  37. #define PRINT(a) printf(a)
  38. #define MXSPEEDSLIDER 1000
  39. #define MINX 0.0
  40. #define MINY 50.0
  41.  
  42. - takeSpot:(float)x:(float)y;
  43. {
  44.     NXRect r = {{0.0,0.0},{0.0,0.0}};
  45.     NXPoint p = {0.0,0.0};
  46.     [spot lockFocus];
  47.     [spot getSize:&r.size];
  48.     r.origin.x = x;
  49.     r.origin.y = y;
  50.     [clearSpot composite:NX_COPY toPoint:&p];
  51.     [sbuffer composite:NX_DOVER fromRect:&r toPoint:&p];
  52.     [spot unlockFocus];
  53.     return self;
  54. }
  55.  
  56. - takeSnapshot;
  57. {
  58.     id snapShot;
  59.     NXSize screen;
  60.     static NXRect imageRect = {{0.0,0.0},{0.0,0.0}};
  61. //    NXColor transBlack = NXChangeAlphaComponent(NX_COLORBLACK,0.33);
  62.     [NXApp getScreenSize:&screen];
  63.     imageRect.size = screen;
  64.     snapShot =[[Window alloc] initContent:&imageRect
  65.         style:NX_PLAINSTYLE
  66.         backing:NX_NONRETAINED
  67.         buttonMask:0
  68.         defer:NO];
  69.     PSsetwindowlevel(NX_MAINMENULEVEL+1,[snapShot windowNum]);
  70.     PSsetautofill(NO,[snapShot windowNum]);
  71.     [snapShot orderFront:self];
  72.     NXPing();
  73.     if (!sbuffer) {
  74.         sbuffer = [[NXImage alloc] initSize:&screen];
  75.     }
  76.     [sbuffer lockFocus];
  77. //    NXSetColor(transBlack);
  78. //    NXRectFill(&imageRect);
  79.     PScomposite(0.0,0.0,screen.width,screen.height,[snapShot gState],
  80.             0.0,0.0,NX_COPY);
  81.     [sbuffer unlockFocus];
  82.     snapShot = [snapShot free];
  83.     return self;
  84. }
  85.  
  86. - oneStep
  87. {
  88.     NXRect black = {0,0,0,0};
  89.     NXRect ballRect;
  90.     BRECT new;
  91.     int mx, my;
  92.  
  93.     then = now;
  94.     now = currentTimeInMs();
  95.  
  96.     my=[speedSlider intValue];                    /*KHO*/
  97.     /* here is a sleep...  This is to safe CUP time. */        /*KHO*/
  98.     /* Perhaps not a very good idea? */                /*KHO*/
  99.     usleep(my*my/10);                        /*KHO*/
  100.     mx=1+(int)((MAX_X_SPEED*(MXSPEEDSLIDER-my))/MXSPEEDSLIDER);    /*KHO*/
  101.     my=1+(int)((MAX_Y_SPEED*(MXSPEEDSLIDER-my))/MXSPEEDSLIDER);    /*KHO*/
  102.  
  103.     xpos = ((1 + sin(((float)now) / X_PERIOD * 2. * PI))/2.0)     /*KHO*/
  104.         * maxCoord.x + MINX;
  105.     ypos = ((1 + sin(((float)now) / Y_PERIOD * 2. * PI))/2.0)     /*KHO*/
  106.         * maxCoord.y + MINY;
  107.     
  108.     if (xpos < (old.l - mx)) xpos = old.l - mx;
  109.     else if (xpos > (old.l + mx)) xpos = old.l + mx;
  110.  
  111.     if (ypos < (old.b - my)) ypos = old.b - my;
  112.     else if (ypos > (old.b + my)) ypos = old.b + my;
  113.  
  114.     new.l = floor(xpos);
  115.     new.b = floor(ypos);
  116.     new.r = new.l + imageSize.width;
  117.     new.t = new.b + imageSize.height;
  118.  
  119.     [self takeSpot:new.l :new.b];
  120.     ballRect.origin.x = 0;
  121.     ballRect.origin.y = 0;
  122.     ballRect.size.width = imageSize.width;
  123.     ballRect.size.height = imageSize.height;
  124.     
  125.     redrawTo.x = MIN(new.l, old.l);
  126.     redrawTo.y = MIN(new.b, old.b);
  127.  
  128.     redraw.origin.x = 0;
  129.     redraw.origin.y = 0;
  130.     redraw.size.width = (MAX(new.r, old.r)) - redrawTo.x + 1;
  131.     redraw.size.height = (MAX(new.t, old.t)) - redrawTo.y + 1;
  132.     
  133.     black.size= redraw.size;
  134.  
  135.     [buffer lockFocus];
  136.     PSsetgray(0);
  137.     NXRectFill(&black);
  138.     
  139.     ballTo.x = new.l - redrawTo.x;
  140.     ballTo.y = new.b - redrawTo.y;
  141.  
  142.     [spot composite:NX_SOVER fromRect:&ballRect toPoint:&ballTo];
  143.     [buffer unlockFocus];
  144.     [buffer composite:NX_COPY fromRect:&redraw toPoint:&redrawTo];
  145.     old = new;
  146.     return self;
  147. }
  148.  
  149.  
  150. - initFrame:(const NXRect *)frameRect
  151. {
  152.     const char *animSpeed;
  153.  
  154.     NXRect black = {0, 0, BUFFER_WIDTH, BUFFER_HEIGHT };
  155.     [super initFrame:frameRect];
  156.     [self allocateGState];        // For faster lock/unlockFocus
  157.     [self setClipping:NO];        // even faster...
  158.     //in this case, I only need one buffer for several Views
  159.     if (!(buffer = [NXImage findImageNamed:"worldBuffer"]))
  160.     {
  161.         buffer = [[NXImage alloc] initSize:&black.size];
  162.         [((NXImage *)buffer) setName:"worldBuffer"];
  163.     }
  164.     if ([buffer lockFocus])
  165.     {
  166.         PSsetgray(0);
  167.         NXRectFill(&black);
  168.         [buffer unlockFocus];
  169.     }
  170.     animSpeed = NXGetDefaultValue([NXApp appName], "animSpeed");
  171.     if (animSpeed == NULL) framesPerSecond = DEFAULT_FPS;
  172.     else framesPerSecond = atoi(animSpeed);
  173.     nextRotationTime = 0;
  174.     [self newViewSize];
  175.     return self;
  176. }
  177.  
  178. - sizeTo:(NXCoord)width :(NXCoord)height
  179. {
  180.     [super sizeTo:width :height];
  181.     [self newViewSize];
  182.     return self;
  183. }
  184.  
  185. - drawSelf:(const NXRect *)rects :(int)count
  186. {
  187. //    [sbuffer composite:NX_COPY fromRect:rects toPoint:&(rects->origin)];
  188.     return self;
  189. }
  190.     
  191.     
  192. - newViewSize
  193. {
  194.     //this is called every time View size changes
  195.     NXRect black = {0, 0, BUFFER_WIDTH, BUFFER_HEIGHT };
  196.  
  197.     then = now = currentTimeInMs();
  198.  
  199.     if (oldSize.width == bounds.size.width &&
  200.             oldSize.height == bounds.size.height)
  201.         return self;
  202.     else
  203.     {
  204.         oldSize.width = bounds.size.width;
  205.         oldSize.height = bounds.size.height;
  206.     }
  207.     
  208.     maxCoord.x = bounds.size.width - imageSize.width - MINX;
  209.     maxCoord.y = bounds.size.height - imageSize.height - MINY;
  210.     if (maxCoord.x < 0) maxCoord.x = 0;
  211.     if (maxCoord.y < 0) maxCoord.y = 0;
  212.  
  213.  
  214.     old.l = old.r = maxCoord.x/2 + MINX;
  215.     old.b = old.t = maxCoord.y/2 + MINY;
  216.     ballTo.x = ballTo.y = 0;
  217.  
  218.     if ([buffer lockFocus])
  219.     {
  220.         PSsetgray(0);
  221.         NXRectFill(&black);
  222.         [buffer unlockFocus];
  223.     }
  224.  
  225.     return self;
  226. }
  227.  
  228. - (const char *)windowTitle
  229. {
  230.     return "StefView";
  231. }
  232.  
  233.  
  234. - inspector:sender
  235. {
  236.     char buf[MAXPATHLEN];
  237.  
  238.     if (!sharedInspectorPanel){
  239.         sprintf(buf,"%s/StefView.nib",[sender moduleDirectory:"Stef"]);
  240.         [NXApp loadNibFile:buf owner:self withNames:NO];
  241.         if (!spot) {
  242.             sprintf(buf,"%s/Spot.tiff",[sender moduleDirectory:"Stef"]);
  243.             spot = [[NXImage alloc] initFromFile:buf];
  244.             [spot lockFocus];
  245.             [spot unlockFocus];
  246.             clearSpot = [[NXImage alloc] initFromFile:buf];
  247.             [clearSpot lockFocus];
  248.             [clearSpot unlockFocus];
  249.             [spot getSize:&imageSize];
  250.         }
  251.     }
  252.     [self takeSnapshot];
  253.     return sharedInspectorPanel;
  254. }
  255.  
  256. @end
  257.